package com.quan.elasticjob.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;

/**
 * 作业配置处理类
 * @author yhaoquan
 *
 */
@Component
public class ElasticJobHandler {

	@Autowired
	private CoordinatorRegistryCenter coordinatorRegistryCenter;

	@Autowired
	private ElasticJobListener elasticJobListener;

	@Autowired
	private JobEventConfiguration jobEventConfiguration;
	
	private int shardingTotalCount = 1 ;

	private static SimpleJobConfiguration simpleJobConfig(String jobName, String cron, int shardingTotalCount, Class<? extends SimpleJob> jobClass, String params) {
		// 定义作业核心配置
		JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(jobName, cron, shardingTotalCount).jobParameter(params).build();

		return new SimpleJobConfiguration(simpleCoreConfig, jobClass.getCanonicalName());

	}
	
	/**
	 * 添加一个定时任务
	 * @param jobName 作业名称
	 * @param cron Cron表达式
	 * @param shardingTotalCount 分片数
	 * @param jobClass 作业实体类
	 * @param params 字符串参数
	 */
	public void addJob(String jobName, String cron, int shardingTotalCount, Class<? extends SimpleJob> jobClass, String params) {
		try {
			shardingTotalCount = shardingTotalCount == 0 ? this.shardingTotalCount : shardingTotalCount ;
			
			// 定义Lite作业根配置
			LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfig(jobName, cron, shardingTotalCount, jobClass, params)).overwrite(true).build();
			// 启动作业
			new SpringJobScheduler(jobClass.newInstance(), coordinatorRegistryCenter, liteJobConfiguration, jobEventConfiguration, elasticJobListener).init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 添加一个定时任务
	 * @param jobName 作业名称
	 * @param cron shardingTotalCount
	 * @param shardingTotalCount 分片数
	 * @param jobClass 作业类路径
	 * @param params 字符串参数
	 */
	@SuppressWarnings("unchecked")
	public void addJob(String jobName, String cron, int shardingTotalCount, String jobClass, String params) {
		try {
			Class<? extends SimpleJob> clazz = (Class<? extends SimpleJob>) Class.forName(jobClass);
			shardingTotalCount = shardingTotalCount == 0 ? this.shardingTotalCount : shardingTotalCount ;
			// 定义Lite作业根配置
			LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfig(jobName, cron, shardingTotalCount, clazz, params)).overwrite(true).build();

			// 启动作业
			new SpringJobScheduler(clazz.newInstance(), coordinatorRegistryCenter, liteJobConfiguration, jobEventConfiguration, elasticJobListener).init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
